package com.project.springboot.strategy;

import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.handler.context.RowWriteHandlerContext;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.poi.ss.util.CellRangeAddress;

import java.util.Map;

/**
 * description: 合并单元格策略
 * @author 朱正磊
 * @date 2023-08-17 11:54:37
 */
@Data
@AllArgsConstructor
public class ExcelMergeStrategy implements RowWriteHandler {

    // 合并的起始行：key：开始，value；结束
    private Map<Integer, Integer> map;

    // 要合并的列
    private int[] cols;

    @Override
    public void afterRowDispose(RowWriteHandlerContext context) {
        // 如果是head或者当前行不是合并的起始行，跳过
        if (context.getHead() || !map.containsKey(context.getRowIndex())) {
            return;
        }
        Integer endRow = map.get(context.getRowIndex());
        if (!context.getRowIndex().equals(endRow)) {
            // 编列合并的列，合并行
            for (int col : cols) {
                // CellRangeAddress(起始行，结束行，起始列，结束列)
                CellRangeAddress cellRangeAddress = new CellRangeAddress(context.getRowIndex(), endRow, col, col);
                context.getWriteSheetHolder().getSheet().addMergedRegionUnsafe(cellRangeAddress);
            }
        }
    }

}
